define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'qrcode', 'layer', 'upload'], function ($, undefined, Backend, Table, Form, qrcode, layer, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['add','edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    // // 要关联的拓展信息
                    // params.extra = 'partner,open_auth';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        {field: 'id', title: __('ID'),operate:false},
                        {field: 'title', title: __('title'),align:'left',operate:"LIKE %...%", formatter: Controller.api.formatter.remark},
                        {field: 'type', title: '类型', formatter: function (value, row, index) {
                            return (row.type == 1) ? '虚拟' : '实物';
                        }},
                        {field: 'point', title: '所需积分',operate: false},
                        {field: 'stock', title: '库存',visible: false,operate: false},
                        {field: 'use_customer', title: '客户经理可否兑换', formatter: function (value, row, index) {
                            return (row.use_customer == 1) ? '可以' : '不可';
                            },operate:false},
                        {field: 'status', title: '状态', formatter: function (value, row, index) {
                            return (row.status == 1) ? '正常' : '下架';
                            },operate:false},
                        {field: 'update_time', title: '最后修改时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add:function () {
            Controller.api.bindevent();
        },
        edit:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on("click", ".fieldlist .append", function () {
                    var rel = parseInt($(this).closest("dl").attr("rel")) + 1;
                    var name = $(this).closest("dl").data("name");
                    $(this).closest("dl").attr("rel", rel);
                    $('<dd class="form-inline"><input type="text" name="' + name + '[field][' + rel + ']" class="form-control" value="" size="10" /> <input type="text" name="' + name + '[value][' + rel + ']" class="form-control" value="" size="40" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>').insertBefore($(this).parent());
                });
                $(document).on("click", ".fieldlist dd .btn-remove", function () {
                    $(this).parent().remove();
                });

                $(document).on("click", "input[name='row[type]']", function () {
                    if($(this).val() == 1) {
                        $("div#goods_stock_box").removeClass('hidden');
                    } else {
                        $("div#goods_stock_box").addClass('hidden');
                    }
                });
            },
            formatter:{
                operate: function (value, row, index) {
                    var _operate = '';
                    return _operate + Table.api.formatter.operate(value, row, index, $("#table"));
                }
            },
            events:{
                operate: $.extend({
                }, Table.api.events.operate)
            }
        }
    }
    return Controller;
});
